package com.punai.monthanalysis.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.externalccb.constants.CCBConstants;
import com.punai.externalccb.domain.YdBqitemMate;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.externalccb.mapper.YdcbFeeitemMateMapper;
import com.punai.monthanalysis.bo.ProYdcbfxRgfdbfxbBo;
import com.punai.monthanalysis.constants.YdcbfxConstants;
import com.punai.monthanalysis.domain.ProYdcbfxRgfdbfxb;
import com.punai.monthanalysis.mapper.ProYdcbfxRgfdbfxbMapper;
import com.punai.monthanalysis.service.IProYdcbfxRgfdbfxbService;
import com.punai.monthanalysis.sqliteDao.CostStructureCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 人工费对比分析Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-19
 */
@Service
public class ProYdcbfxRgfdbfxbServiceImpl extends ServiceImpl<ProYdcbfxRgfdbfxbMapper,ProYdcbfxRgfdbfxb> implements IProYdcbfxRgfdbfxbService
{
    @Autowired
    private ProYdcbfxRgfdbfxbMapper proYdcbfxRgfdbfxbMapper;
    @Autowired
    private YdcbFeeitemMateMapper ydcbFeeitemMateMapper;
    @Autowired
    private CostStructureCategoryRepository costStructureCategoryRepository;


    @Override
    public List<? extends BaseEntity> getTabsDataList(String proId,String monthId,List<YdCbfeeiteminfoStandard> list,List<YdCbfeeiteminfoStandard> categorysList) {
        List<ProYdcbfxRgfdbfxbBo> result = new ArrayList<>();
        List<ProYdcbfxRgfdbfxbBo> allList = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)){
            for (YdCbfeeiteminfoStandard standard : list) {
                ProYdcbfxRgfdbfxbBo convert = convert(standard);
                allList.add(convert);
            }
        }

        List<Integer> oids = list.stream().map(YdCbfeeiteminfoStandard::getOid).filter(Objects::nonNull).collect(Collectors.toList());
        List<YdBqitemMate> rgfdbfx = proYdcbfxRgfdbfxbMapper.getRgfdbfx(proId,monthId,CCBConstants.CCB_TYPE.YDCBFX.getCode(),oids);
        Map<Integer, List<YdBqitemMate>> reltypeMap = rgfdbfx.stream().filter(v->v.getReltype()!=null).collect(Collectors.groupingBy(YdBqitemMate::getReltype, Collectors.toList()));
        if (CollUtil.isNotEmpty(reltypeMap)){
            for (Map.Entry<Integer, List<YdBqitemMate>> entry : reltypeMap.entrySet()) {
                Integer key = entry.getKey();
                String desc = YdcbfxConstants.TABS_RGFDBFXB_DATATYPE.getDescByDataType(key.toString());
                List<YdBqitemMate> value = entry.getValue();
                List<ProYdcbfxRgfdbfxbBo> collect = allList.stream().filter(
                        a ->
                                value.stream().map(YdBqitemMate::getCbfeeiteminfoid)
                                        .anyMatch(
                                                infoId -> Objects.equals(a.getId(), infoId)
                                        )
                ).collect(Collectors.toList());
                BigDecimal ysAmount = collect.stream().map(ProYdcbfxRgfdbfxb::getYsQuantity).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
                BigDecimal ysAvgPrice = collect.stream().map(ProYdcbfxRgfdbfxb::getYsAvgPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
                BigDecimal ysPrice = collect.stream().map(ProYdcbfxRgfdbfxb::getYsPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
                BigDecimal sjAmount = collect.stream().map(ProYdcbfxRgfdbfxb::getSjQuantity).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
                BigDecimal sjAvgPrice = collect.stream().map(ProYdcbfxRgfdbfxb::getSjAvgPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
                BigDecimal sjPrice = collect.stream().map(ProYdcbfxRgfdbfxb::getSjPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
                BigDecimal jcQuantity = NumberUtil.subtractNew(ysAmount, sjAmount);
                BigDecimal jcQuantityJcl = NumberUtil.divideNew(jcQuantity, 2, ysAmount);
                BigDecimal price = NumberUtil.subtractNew(ysPrice, sjPrice);
                BigDecimal priceJcl = NumberUtil.divideNew(price, 2, ysPrice);
                ProYdcbfxRgfdbfxbBo bo = new ProYdcbfxRgfdbfxbBo();
                bo.setId(UUID.randomUUID()+"");
                bo.setItemName(desc);
                bo.setYsPrice(ysPrice);
                bo.setSjPrice(sjPrice);
                bo.setPrice(price);
                bo.setChildren(collect);
                result.add(bo);
            }
        }
        return result;
    }
    private void setChildren(ProYdcbfxRgfdbfxbBo parent,
                             ListValueMap<String, YdCbfeeiteminfoStandard> listValueMap) {

        String id = parent.getId();
        List<YdCbfeeiteminfoStandard> children = listValueMap.get(id);
        if (CollUtil.isNotEmpty(children)) {
            for (YdCbfeeiteminfoStandard child : children) {
                ProYdcbfxRgfdbfxbBo convert = convert(child);
                parent.getChildren().add(convert);
                setChildren(convert,listValueMap);
            }
        }
    }


    private ProYdcbfxRgfdbfxbBo convert(YdCbfeeiteminfoStandard in) {
        ProYdcbfxRgfdbfxbBo out = new ProYdcbfxRgfdbfxbBo();
        out.setId(in.getId());
        out.setProId(in.getProId());
        out.setQc(in.getYdDate());//期次Id
        out.setItemName(in.getName());
        out.setUnit(in.getUnit());

        out.setYsQuantity(in.getMonthAmount());
        out.setYsAvgPrice(in.getMonthPrice());
        out.setYsPrice(in.getMonthCzTotal());

        out.setSjQuantity(in.getActualCostAmout());
        out.setSjAvgPrice(in.getActualCostNoTaxPrice());
        out.setSjPrice(in.getActualCostNoTaxTotal());

        BigDecimal ysAmount = in.getMonthAmount();
        BigDecimal ysTotal = in.getMonthCzTotal();
        BigDecimal jcQuantity = in.getDifferenceOfCzsjAmount();
        BigDecimal quantityJcl = NumberUtil.divideNew(jcQuantity, 2, ysAmount);
        BigDecimal price = in.getKlDifferenceOfCzsjTotal();
        BigDecimal priceJcl = NumberUtil.divideNew(price, 2, ysTotal);
        out.setJcQuantity(jcQuantity);
        out.setQuantityJcl(quantityJcl);
        out.setPrice(price);
        out.setPriceJcl(priceJcl);
        return out;
    }
}
